//
//  ListCollectionViewCell.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/6.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "ListCollectionViewCell.h"

@implementation ListCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self getData];
        [self creatTableView];
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return self;
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    self.tableView.clipsToBounds = YES;
    self.tableView.bounces = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 150)];
    
    [self.tableView registerClass:[ListTableViewCell class] forCellReuseIdentifier:@"ListTableViewCell"];
    
    [self addSubview:self.tableView];
}

-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.imageViews.frame = self.tableView.tableHeaderView.frame;
}

- (void)getData {
    
    NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
    [SANetWorkingTask request:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=and-c3&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.38.2" parmater:nil block:^(id result) {
        self.picUrl = [result[@"focusImages"][@"list"] lastObject][@"pic"];
        
        self.totalArray = [NSArray arrayWithArray:result[@"datas"]];
        
        for (int i = 0; i < self.totalArray.count; i++) {
            [mutableArray addObject:[self.totalArray[i] objectForKey:@"title"]];
        }
        self.titleArray = [NSArray arrayWithArray:mutableArray];
        
        NSArray *first = [NSArray arrayWithArray:[result[@"datas"] firstObject][@"list"]];
        
        NSArray *second = [NSArray arrayWithArray:[result[@"datas"] lastObject][@"list"]];
        
        NSMutableArray *firstMutable = [NSMutableArray arrayWithCapacity:0];
        
        for (NSDictionary *dic in first) {
            ListModel *model = [[ListModel alloc] initWithDictionary:dic];
            [firstMutable addObject:model];
        }
        
        self.firstArray = [NSArray arrayWithArray:firstMutable];
        
        [firstMutable removeAllObjects];
        
        for (NSDictionary *dic in second) {
            ListModel *model = [[ListModel alloc] initWithDictionary:dic];
            [firstMutable addObject:model];
        }
        
        self.secondArray = [NSArray arrayWithArray:firstMutable];
        
        [self.dataSource addObject:self.firstArray];
        [self.dataSource addObject:self.secondArray];
        
        [self getTableVIewHeadView];
        
        [self.tableView reloadData];
    }];
    
}

- (void)getTableVIewHeadView {
    self.imageViews = [[UIImageView alloc] initWithFrame:self.tableView.tableHeaderView.frame];
    [self.imageViews sd_setImageWithURL:[NSURL URLWithString:self.picUrl]];
    [self.tableView.tableHeaderView addSubview:self.imageViews];
}

#pragma mark -- tableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListTableViewCell"];
    
    if (indexPath.section == 0) {
        cell.listModel = self.firstArray[indexPath.row];
    } else {
        cell.listModel = self.secondArray[indexPath.row];
    }
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.totalArray.count) {
        NSDictionary *dic = self.totalArray[section];
        NSArray *array = [dic objectForKey:@"list"];
        
        return array.count;
    }
    return 0;
}

#pragma mark -- tableViewDelegata
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.titleArray.count) {
        return self.titleArray.count;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    tableViewHeaderView *view = [[tableViewHeaderView alloc] init];
    if (self.titleArray.count) {
        view.titleLabel.text = self.titleArray[section];
    }
    
    return view;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ListModel *model = self.dataSource[indexPath.section][indexPath.row];
    ListDetailViewController *viewC = [[ListDetailViewController alloc] init];
    viewC.url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=android&key=%@&pageId=1&pageSize=40",model.contentType,model.key];
    viewC.contentType = model.contentType;
    viewC.key = model.key;
    [self.delegate changeVC:viewC.url withcontentType:viewC.contentType];
    
}


@end
